package com.hontech.bread.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hontech.bread.R
import com.hontech.bread.app.runWithInit
import com.hontech.bread.store.StoreProduct
import com.hontech.bread.store.setImageWithByteArray
import com.hontech.bread.ui.dialog.readImage
import com.hontech.bread.ui.dialog.runProgErr
import com.hontech.bread.ui.view.LabelTextInput
import com.hontech.bread.utils.EmptyEdge
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.priceFormat
import com.hontech.bread.utils.priceFormat2
import com.hontech.bread.utils.runOnUiCatchAny
import com.hontech.bread.utils.stringById

class CreateProductFragment : Fragment() {

    private lateinit var ltName: LabelTextInput
    private lateinit var ltPrice: LabelTextInput
    private lateinit var ltHeatTime: LabelTextInput
    private lateinit var ltDiedDate: LabelTextInput
    private lateinit var btnSelectImage: Button
    private lateinit var btnCreate: Button
    private lateinit var btnUpdate: Button
    private lateinit var btnDelete: Button
    private lateinit var imageView: ImageView
    private lateinit var mRv: RecyclerView
    private lateinit var mAdapter: CpAdapter

    private var selectImage: ByteArray? = null
    private var selectProduct: StoreProduct? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_create_product, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {
        ltName = view.findViewById(R.id.id_cp_lti_name)
        ltPrice = view.findViewById(R.id.id_cp_lti_price)
        ltHeatTime = view.findViewById(R.id.id_cp_lti_heat_time)
        ltDiedDate = view.findViewById(R.id.id_cp_lti_died_date)
        btnSelectImage = view.findViewById(R.id.id_cp_btn_select_image)
        btnCreate = view.findViewById(R.id.id_cp_btn_create)
        btnUpdate = view.findViewById(R.id.id_cp_btn_update)
        btnDelete = view.findViewById(R.id.id_cp_btn_delete)
        imageView = view.findViewById(R.id.id_cp_image)
        mRv = view.findViewById(R.id.id_cp_rv)
        mAdapter = CpAdapter(::onClickItem)

        mRv.edgeEffectFactory = EmptyEdge()
        mRv.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        mRv.adapter = mAdapter

        btnCreate.onClick { onCreate() }
        btnSelectImage.onClick { onSelectImage() }
        btnUpdate.onClick { onUpdate() }
        btnDelete.onClick { onDelete() }

    }

    override fun onResume() {
        super.onResume()
        runOnUiCatchAny { runWithInit { mAdapter.update() } }
    }

    private fun onClickItem(product: StoreProduct) {
        selectProduct = product
        ltName.setText(product.name)
        ltPrice.setText(priceFormat2(product.price.toLong()))
        ltHeatTime.setText(product.heatTime.toString())
        ltDiedDate.setText((product.expiration / 24 / 3600).toString())
        selectImage = product.image
        val image = product.image
        imageView.setImageWithByteArray(image)
    }

    private suspend fun onCreate() {

    }

    private suspend fun onUpdate() {

    }

    private suspend fun onSelectImage() {
        runProgErr(stringById(R.string.t1733460848_328)) {
            val image = try {
                readImage()
            } catch (e: Exception) {
                return@runProgErr
            }
            selectImage = image
            imageView.setImageWithByteArray(image)
        }
    }

    private suspend fun onDelete() {

    }

}

private class CpItem(view: View) : RecyclerView.ViewHolder(view) {

    private val tvName = view.findViewById<TextView>(R.id.id_item_cp_tv_name)
    private val tvPrice = view.findViewById<TextView>(R.id.id_item_cp_tv_price)
    private val tvHeatTime = view.findViewById<TextView>(R.id.id_item_cp_tv_heat_time)
    private val tvDiedDate = view.findViewById<TextView>(R.id.id_item_cp_tv_died_date)

    fun setUp(product: StoreProduct, cb: (StoreProduct) -> Unit) {
        tvName.text = product.name
        tvPrice.text = priceFormat(product.price.toLong())
        tvHeatTime.text = product.heatTime.toString()
        tvDiedDate.text = (product.expiration / 24 / 3600).toString()

        itemView.setOnClickListener { cb(product) }
    }
}


private class CpAdapter(val cb: (StoreProduct) -> Unit) : RecyclerView.Adapter<CpItem>() {

    private var list: List<StoreProduct>? = null

    @SuppressLint("NotifyDataSetChanged")
    suspend fun update() {
        list = StoreProduct.query()
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    override fun onBindViewHolder(holder: CpItem, position: Int) {
        val product = list?.get(position) ?: return
        holder.setUp(product, cb)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CpItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cp, parent, false)
        return CpItem(view)
    }
}
















